package com.api.customization.huangwm.formmode.SW003;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;

import com.alibaba.fastjson.JSON;
import com.api.customization.huangwm.dao.SW003.AssetInfoOperationDao;
import com.customization.huangwm.utils.LogTool;
import com.engine.common.util.ParamUtil;

/**
 * @Author Huangwm
 * @Date 2022-04-17
 * @Version 1.0
 * @description TODO 物料、资产信息操作  新增，更新、封存、解封、出入库
 */
public class AssetInfoOperation {

    private LogTool logTool = new LogTool("/log/SW003/huangwm/api/AssetInfoOperation", false);
    private AssetInfoOperationDao apiDao = new AssetInfoOperationDao();

    /**
     * 新增保存
     * @param request
     * @param response
     * @return
     */
    @POST
    @Path("/save")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> Save(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apiResult = new HashMap<String, Object>();
        try {
            Map<String, Object> requestParams = ParamUtil.request2Map(request);
            logTool.writeLog("新增保存");
            logTool.writeLog(JSON.toJSONString(requestParams));
            apiResult = apiDao.Save(requestParams);
        } catch (Exception e) {
            e.printStackTrace();
            apiResult.put("msg", "保存物料異常 " + e.toString());
            apiResult.put("apiStatus", false);
        }
        logTool.writeLog(JSON.toJSONString(apiResult));
        return apiResult;
    }

    /**
     * 更新
     * @param request
     * @param response
     * @return
     */
    @POST
    @Path("/update")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> Update(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apiResult = new HashMap<String, Object>();
        try {
            Map<String, Object> requestParams = ParamUtil.request2Map(request);
            logTool.writeLog("更新");
            logTool.writeLog(JSON.toJSONString(requestParams));
            apiResult = apiDao.Update(requestParams);
        } catch (Exception e) {
            e.printStackTrace();
            apiResult.put("msg", "更新物料異常 " + e.toString());
            apiResult.put("apiStatus", false);
        }
        logTool.writeLog(JSON.toJSONString(apiResult));
        return apiResult;
    }

    /**
     * 生效、失效
     * @param request
     * @param response
     * @return
     */
    @POST
    @Path("/status")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> Sealed(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apiResult = new HashMap<String, Object>();
        try {
            Map<String, Object> requestParams = ParamUtil.request2Map(request);
            logTool.writeLog("生效、失效");
            logTool.writeLog(JSON.toJSONString(requestParams));
            apiResult = apiDao.Status(requestParams);
        } catch (Exception e) {
            e.printStackTrace();
            apiResult.put("msg", "物料狀態更新異常 " + e.toString());
            apiResult.put("apiStatus", false);
        }
        logTool.writeLog(JSON.toJSONString(apiResult));
        return apiResult;
    }

    /**
     * 出入库
     * @param request
     * @param response
     * @return
     */
    @POST
    @Path("/updateInventory")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> UpdateInventory(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apiResult = new HashMap<String, Object>();
        try {
            Map<String, Object> requestParams = ParamUtil.request2Map(request);
            logTool.writeLog("出入庫");
            logTool.writeLog(JSON.toJSONString(requestParams));
            //apiResult = apiDao.UpdateInventory(requestParams);
            apiResult = apiDao.UpdateInventoryV2(requestParams);
        } catch (Exception e) {
            e.printStackTrace();
            apiResult.put("msg", "更新物料庫存異常 " + e.toString());
            apiResult.put("apiStatus", false);
        }
        logTool.writeLog(JSON.toJSONString(apiResult));
        return apiResult;
    }

    /**
     * 采購入庫 isAsset為Y則進行資產登記，N則直接入庫數量并且生成入庫記錄
     * @param request
     * @param response
     * @return
     */
    @POST
    @Path("/purchaseStorage")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> PurchaseStorage(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apiResult = new HashMap<String, Object>();
        try {
            Map<String, Object> requestParams = ParamUtil.request2Map(request);
            logTool.writeLog("采購入庫");
            logTool.writeLog(JSON.toJSONString(requestParams));
            apiResult = apiDao.PurchaseStorage(requestParams);
        } catch (Exception e) {
            e.printStackTrace();
            apiResult.put("msg", "采購入庫異常 " + e.toString());
            apiResult.put("apiStatus", false);
        }
        logTool.writeLog(JSON.toJSONString(apiResult));
        return apiResult;
    }

    /**
     * 采購退貨 isAsset為Y則進行資產登記，N則直接入庫數量并且生成入庫記錄
     *
     * @param request
     * @param response
     * @return
     */
    @POST
    @Path("/purchaseStorageReturn")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> PurchaseStorageReturn(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apiResult = new HashMap<String, Object>();
        try {
            Map<String, Object> requestParams = ParamUtil.request2Map(request);
            logTool.writeLog("采購入庫退貨");
            logTool.writeLog(JSON.toJSONString(requestParams));
            apiResult = apiDao.PurchaseStorageReturn(requestParams);
        } catch (Exception e) {
            e.printStackTrace();
            apiResult.put("msg", "采購入庫退貨異常 " + e.toString());
            apiResult.put("apiStatus", false);
        }
        logTool.writeLog(JSON.toJSONString(apiResult));
        return apiResult;
    }
}
